from lxml import etree
import requests

# 需求：获取58同城二手房数据

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
}
url = 'https://bj.58.com/ershoufang/?from=esf&from=esf_list'
# 请求二手房网页
page = requests.get(url=url, headers=headers).text
# 实例化xpath对象
tree = etree.HTML(page)
section_list = tree.xpath('//section[@class="list"]/div[@class="property"]')
# print(section_list)
fp = open('58.txt','w')
for section in section_list:
    data = section.xpath('.//h3/text()')[0]
    fp.write(data+'\n')
fp.close()





